#=========================================================================
# Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=========================================================================

project(ComoTools)

cmake_minimum_required(VERSION 2.8)

set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "" CACHE STRING "" FORCE)
set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "" CACHE STRING "" FORCE)

if($ENV{ARCH} MATCHES "(x64)|(aarch64)")
    #! -DARM_FP_SUPPORT, Architecture-related options
    set(COMMON_C_FLAGS
        "-fPIC -ffunction-sections -fdata-sections ${FUNCTION_SAFETY_C_FLAGS} -DARM_FP_SUPPORT")
elseif($ENV{ARCH} MATCHES "(riscv64)")
    set(COMMON_C_FLAGS
        "-fPIC -ffunction-sections -fdata-sections ${FUNCTION_SAFETY_C_FLAGS}")
elseif($ENV{ARCH} MATCHES "(x32)|(aarch32)|(riscv32)")
    set(COMMON_C_FLAGS
        "-m32 -fPIC -ffunction-sections -fdata-sections ${FUNCTION_SAFETY_C_FLAGS}")
elseif($ENV{ARCH} MATCHES "(aarch32)|(riscv32)")
    set(COMMON_C_FLAGS
        "-fPIC -ffunction-sections -fdata-sections ${FUNCTION_SAFETY_C_FLAGS}")
else()
    set(COMMON_C_FLAGS
        "-fPIC -ffunction-sections -fdata-sections ${FUNCTION_SAFETY_C_FLAGS}")
endif()

set(COMMON_CXX_FLAGS
    "${COMMON_C_FLAGS} -fno-exceptions -fno-rtti -std=gnu++11")
set(COMMON_SHARED_LINKER_FLAGS
    "-shared -fPIC ${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections -Wl,--no-undefined,--no-undefined-version -Wl,--hash-style=gnu")
set(COMMON_EXE_LINKER_FLAGS
    "-Bdynamic -pie ${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -Wl,-z,nocopyreloc")

set(CMAKE_CXX_FLAGS
    "${COMMON_CXX_FLAGS} -fvisibility=hidden -Os -g" CACHE STRING "" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS
    "${COMMON_SHARED_LINKER_FLAGS} -Wl,--strip-all" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS
    "${COMMON_EXE_LINKER_FLAGS} -Wl,--strip-all" CACHE STRING "" FORCE)

include($ENV{ROOT}/build/common.cmake)

set(CMAKE_VERBOSE_MAKEFILE off)
set(PROJECT_DIR ${CMAKE_SOURCE_DIR})

set( THREADS_PTHREAD_ARG
     "PLEASE_FILL_OUT-FAILED_TO_RUN"
     CACHE STRING "Result from TRY_RUN" FORCE)

set(TOOLS_DIR $ENV{ROOT}/tools)
set(EXTERNAL_DIR $ENV{ROOT}/external)
set(BIN_DIR $ENV{BIN_PATH})
set(INC_DIR ${BIN_DIR}/inc)

set(COMOTOOLS_SRC_DIR ${PROJECT_SOURCE_DIR})
set(COMOTOOLS_OBJ_DIR ${PROJECT_SOURCE_DIR}/out)
set(COMOTOOLS_BIN_DIR ${PROJECT_SOURCE_DIR}/bin)

add_subdirectory(comotools)
